
import api from '../api/index.js'
import fetch from 'isomorphic-fetch'
import queryString from 'query-string'
import utils from '@/plugins/utils'
import Cookies from 'js-cookie'
import Router from '@/router'
async function checkLogin(res){
  // console.log(res)
  let resData = await res
  if(+resData.ErrorCode == 10001){
    Router.replace({
      path: '/login'
    })
    return resData
  } else {
    return resData
  }
}
export default {
  async uploadImg({ commit }, data) { //
    const res = await fetch(`${api.uploadImg}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async postTbDistributor({ commit }, data) { //
    const res = await fetch(`${api.postTbDistributor}`, {
      method: "POST",
      credentials: 'omit',
      headers: {
        'Content-Type': 'application/json-patch+json',
        'Authorization': `Bearer ${Cookies('token')}`
      },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async loadDistributor({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.getTbDistributorDropList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async putTbDistributor({ commit }, data) { //
    const res = await fetch(`${api.putTbDistributor}`, {
      method: "PUT",
      credentials: 'omit',
      headers: {
        'Content-Type': 'application/json-patch+json',
        'Authorization': `Bearer ${Cookies('token')}`
      },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async postTbGoods({ commit }, data) { //
    const res = await fetch(`${api.postTbGoods}`, {
      method: "POST",
      credentials: 'omit',
      headers: {
        'Content-Type': 'application/json-patch+json',
        'Authorization': `Bearer ${Cookies('token')}`
      },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async getTbGoodsList({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.getTbGoodsList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async PostTbClassify({ commit }, data) { //
    const res = await fetch(`${api.PostTbClassify}`, {
      method: "POST",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async GetTbClassifyList({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbClassifyList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },


  async DeleteTbClassify({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.DeleteTbClassify}?${query}`, {
      method: "DELETE",
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      },
      mode: "cors",
    })
    return checkLogin(res.json())
  },

  async PutTbClassify({ commit }, data) { //
    const res = await fetch(`${api.PutTbClassify}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },


  async GetTbClassifyDropList({ commit }, data) { //
    const res = await fetch(`${api.GetTbClassifyDropList}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },



  async PutTbGoods({ commit }, data) { //
    const res = await fetch(`${api.PutTbGoods}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },



  async GetTbCardList({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbCardList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async PostTbCards({ commit }, data) { //
    const res = await fetch(`${api.PostTbCards}`, {
      method: "POST",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async GetTbExchangeList({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbExchangeList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async GetTbExchange({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbExchange}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async GetTbUserList({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbUserList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async GetTbAdminUserList({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbAdminUserList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },


  async DeleteTbAdminUser({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.DeleteTbAdminUser}?${query}`, {
      method: "DELETE",
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      },
      mode: "cors",
    })
    return checkLogin(res.json())
  },


  async PutTbAdminUserPwd({ commit }, data) { //
    const res = await fetch(`${api.PutTbAdminUserPwd}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },


  async PostTbAdminUser({ commit }, data) { //
    const res = await fetch(`${api.PostTbAdminUser}`, {
      method: "POST",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async GetThemeList({ commit }, data) { // 主题列表
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetThemeList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async GetThemeInfo({ commit }, data) { // 主题详情
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetThemeInfo}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async PutTheme({ commit }, data) { //
    const res = await fetch(`${api.PutTheme}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async PostTheme({ commit }, data) { //
    const res = await fetch(`${api.PostTheme}`, {
      method: "POST",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },
  async postImg({ commit }, data) { //
    const res = await fetch(`https://upload.qiniup.com`, {
      method: "POST",
      credentials: 'omit',
      // headers: {'Content-Type': 'multipart/form-data'},
      mode: "cors",
      body: data
    })
    return checkLogin(res.json())
  },

  async GetTbCardCardBagNumberList({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbCardCardBagNumberList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async GetTbGoodsDropList({ commit }, data) { //
    const res = await fetch(`${api.GetTbGoodsDropList}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async GetEclpGoodsQueryGoodsInfo({ commit }, data) { //
    const res = await fetch(`${api.GetEclpGoodsQueryGoodsInfo}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async GetTbOrdersList({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbOrdersList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async GetTbOrders({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbOrders}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async GetLogistics({ commit }, data) { //获取快递信息
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetLogistics}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async GetComplaintList({ commit }, data) { //获取投诉信息
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetComplaintList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async PutComplaintList({ commit }, data) { // 处理投诉信息
    const res = await fetch(`${api.PutComplaintList}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },
  async PutCardActive({ commit }, data) { // 批量修改卡激活状态
    const res = await fetch(`${api.PutCardActive}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async putRecevied({ commit }, data) { // 发货
    const res = await fetch(`${api.putRecevied}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },
  async PutCardFrozens({ commit }, data) { // 批量冻结
    const res = await fetch(`${api.PutCardFrozens}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async GetTbGoods({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbGoods}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async getRecevieType({ commit }, data) { //获取快递类型
    let query = queryString.stringify(data)
    const res = await fetch(`${api.getRecevieType}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async getTimeConfig({ commit }, data) { //获取定时时间
    let query = queryString.stringify(data)
    const res = await fetch(`${api.getTimeConfig}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async putTimeConfig({commit}, data){
    const res = await fetch(`${api.putTimeConfig}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },
  async loginUser({commit}, data){
    let query = queryString.stringify(data)
    const res = await fetch(`${api.loginUser}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async GetTbExchangeBigNumList({ commit }, data) { //获取投诉信息
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbExchangeBigNumList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async  invoiceLogList({ commit }, data) { //获取开票列表
    let query = queryString.stringify(data)
    const res = await fetch(`${api.invoiceLogList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async  invoiceLogInfo({ commit }, data) { //获取开票详情
    let query = queryString.stringify(data)
    const res = await fetch(`${api.invoiceLogInfo}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async getRecevieList({ commit }, data) { //获取快递列表
    let query = queryString.stringify(data)
    const res = await fetch(`${api.getRecevieList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async bigExport({ commit }, data) { //获取大件兑换导出
    let query = queryString.stringify(data)
    const res = await fetch(`${api.bigExport}?${query}`, {
      // credentials: 'omit',,
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      },
      method: 'get',
      responseType: 'blob'
  })
    return res.blob();
  },
  async bigExpres({ commit }, data) { //大件兑换导入
    let query = queryString.stringify(data)
    const res = await fetch(`${api.bigExpres}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: data
    })
    return checkLogin(res.json())
  },
  async getCarouseList({ commit }, data) { //获取轮播图列表
    let query = queryString.stringify(data)
    const res = await fetch(`${api.getCarouseList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async setCarouse({ commit }, data) { //修改轮播图
    let query = queryString.stringify(data)
    const res = await fetch(`${api.setCarouse}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },
  async postCarouse({ commit }, data) { //修改轮播图
    const res = await fetch(`${api.postCarouse}`, {
      method: "POST",
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },
  async delCarouse({ commit }, data) { //删除轮播图
    let query = queryString.stringify(data)
    const res = await fetch(`${api.delCarouse}?${query}`, {
      method: "DELETE",
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      },
      credentials: 'omit',
      mode: "cors"
    })
    return checkLogin(res.json())
  },

  async GetTbExpressTypeList({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbExpressTypeList}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },

  async DeleteTbExpressType({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.DeleteTbExpressType}?${query}`, {
      method: "DELETE",
      credentials: 'omit',
      mode: "cors",
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      },
    })
    return checkLogin(res.json())
  },
  async PutTbOrdersRefunded({ commit }, data) { //
    const res = await fetch(`${api.PutTbOrdersRefunded}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json',
      'Authorization': `Bearer ${Cookies('token')}` },
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },

  async putInvoiceLogInfo({ commit }, data) { //修改开票状态
    const res = await fetch(`${api.putInvoiceLogInfo}`, {
      method: "PUT",
      credentials: 'omit',
      headers: { 'Content-Type': 'application/json-patch+json' ,
      'Authorization': `Bearer ${Cookies('token')}`},
      mode: "cors",
      body: JSON.stringify(data)
    })
    return checkLogin(res.json())
  },
  async GetTbCard({ commit }, data) { //
    let query = queryString.stringify(data)
    const res = await fetch(`${api.GetTbCard}?${query}`, {
      credentials: 'omit',
      headers:{
        'Authorization': `Bearer ${Cookies('token')}`
      }
    })
    return checkLogin(res.json())
  },
  async getWxCode({ commit }, data){
    let query = queryString.stringify(data)
    const res = await fetch(`${api.getWxCode}?${query}`, {
      credentials: 'omit',
      headers: {
      'Authorization': `Bearer ${Cookies('token')}`},
    })
    return res.text();

  }
}
